//
//  LKSeeBillListViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/6/30.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKSeeBillListViewController.h"
#import "LKBillListCell.h"
#import "LKBillDetailViewController.h"
#import "LKDatePickerCell.h"
#import "LKBillListModel.h"
#import "LKAccountModel.h"LKSeeBillListViewController
#import "GFDatePicker.h"
@interface LKSeeBillListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *updateDataArray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)NSMutableDictionary *dataDic;
@property(nonatomic,strong)NSMutableArray *rolesArray;
@property(nonatomic,strong)NSMutableDictionary *paramDic;
@property(nonatomic,copy)NSString *begin_time;
@property(nonatomic,copy)NSString *end_time;
@property(nonatomic,copy)NSString *key_word;
@property(nonatomic,copy)NSString *paystyle;
@property(nonatomic,copy)NSString *status;
@property(nonatomic,copy)NSString *mode;
@property(nonatomic,copy)NSString *type;
@property(nonatomic,copy)NSString *checker_id;
@property(nonatomic,strong)WMZDropDownMenu *menu;
@property(nonatomic,assign)BOOL shaixuanflag;
@end

@implementation LKSeeBillListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.shaixuanflag = NO;
    self.begin_time = @"";
    self.end_time = @"";
    self.paystyle = @"";
    self.status = @"";
    self.key_word = @"";
    self.mode = @"";
    self.type = @"0";
    self.checker_id = @"";
    self.navigationItem.title = @"流水账单";
    if (self.cate_id) {
        self.page = 1;
        self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"cate_id":self.cate_id,@"page":@(self.page)}];
        [self getPayBillListWithTypeParam];
    }else{
        self.page = 1;
        [self getRolesArrayData];
        self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"page":@(self.page),@"type":self.type,@"begin_time":self.begin_time,@"end_time":self.end_time,@"paystyle":self.paystyle,@"status":self.status,@"mode":self.mode,@"checker_id":self.checker_id,@"key_word":self.key_word}];
        [self getPayBillListWithTypeParam];
    }

    [self createTableview];
    // Do any additional setup after loading the view.
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"筛选" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {

    }];
    WMZDropMenuParam *param =
    MenuParam()
    .wMainRadiusSet(0)
    .wCollectionViewCellSelectTitleColorSet(COLOR_HEX(0x333333))
    .wMenuTitleEqualCountSet(0)
    .wDefaultConfirmHeightSet(50)
    .wReginerCollectionCellsSet(@[@"LKDatePickeCell"])
    .wCollectionViewCellSelectBgColorSet(MenuColor(0xffeceb));
    _menu = [[WMZDropDownMenu alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, 64, 40) withParam:param];
    _menu.delegate = self;
    _menu.titleView.backgroundColor = self.navigationController.navigationBar.barTintColor;
    _menu.backgroundColor = self.navigationController.navigationBar.barTintColor;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_menu];
}
-(void)createTableview{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKBillListCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = viewDefualtColor;
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(NAVIGATION_BAR_HEIGHT + 10);
        make.bottom.equalTo(self.view).offset(0);
    }];

    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self.tableView reloadData];
}
/// 下拉刷新
-(void)headerReloadData{
    if (self.cate_id) {
        self.page = 1;
        self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"cate_id":self.cate_id,@"page":@(self.page)}];

    }else{
        self.page = 1;
        self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"page":@(self.page),@"type":self.type,@"begin_time":self.begin_time,@"end_time":self.end_time,@"paystyle":self.paystyle,@"status":self.status,@"mode":self.mode,@"checker_id":self.checker_id,@"key_word":self.key_word}];
    }
    self.tableView.mj_footer.hidden = NO;
    [self getPayBillListWithTypeParam];
}
/// 上拉加载更多
-(void)footerReloadData{
    self.page++;
    if (self.cate_id) {
        self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"cate_id":self.cate_id,@"page":@(self.page)}];

    }else{
        self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"page":@(self.page),@"type":self.type,@"begin_time":self.begin_time,@"end_time":self.end_time,@"paystyle":self.paystyle,@"status":self.status,@"mode":self.mode,@"checker_id":self.checker_id,@"key_word":self.key_word}];
    }
    [self getPayBillListWithTypeParam];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.updateDataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dic = self.updateDataArray[section];
    NSArray *array = dic[@"list"];
    return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKBillListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKBillListCell"];
    NSDictionary *dic = self.updateDataArray[indexPath.section];
    NSArray *array = dic[@"list"];
    array = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        LKBillListModel *model1 = obj1;
        LKBillListModel *model2 = obj2;
        return [model2.paytime compare:model1.paytime];
    }];
    cell.model = array[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 62.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] init];
    for (UIView *subview in header.subviews) {
        [subview removeFromSuperview];
    }

    LKBaseLabel *leftLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"" textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Mediun" size:16] textAlignment:NSTextAlignmentCenter];
    [header addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(header.mas_left).offset(20);
        make.centerY.equalTo(header);
    }];
    LKBaseLabel *rightLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"" textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentCenter];
    [header addSubview:rightLabel];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(header.mas_right).offset(-20);
        make.centerY.equalTo(header);
    }];
    NSDictionary *dic = self.updateDataArray[section];
    NSMutableString *timeStr = [[NSMutableString alloc] initWithString:dic[@"date"]];
    [timeStr insertString:@"-" atIndex:4];
    [timeStr insertString:@"-" atIndex:7];
    leftLabel.text = timeStr;
    NSArray *array = dic[@"list"];
    double price = 0.0f;
    for (LKBillListModel *model in array) {

        if ([model.status isEqualToString:@"5"]) {
            price -= [model.price floatValue];
        }else{
            price += [model.price floatValue];
        }
        
    }
    rightLabel.text = [NSString stringWithFormat:@"收入￥%.2lf",price];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 36.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.updateDataArray[indexPath.section];
    NSArray *array = dic[@"list"];
    array = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        LKBillListModel *model1 = obj1;
        LKBillListModel *model2 = obj2;
        return [model2.paytime compare:model1.paytime];
    }];
    LKBillDetailViewController *vc = [[LKBillDetailViewController alloc] init];
    vc.model = array[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -menu代理
- (NSArray*)titleArrInMenu:(WMZDropDownMenu *)menu{
    return @[
         @"",@"筛选"
    ];
}


- (NSInteger)menu:(WMZDropDownMenu *)menu numberOfRowsInSection:(NSInteger)section{
    return 6;
}

- (NSArray *)menu:(WMZDropDownMenu *)menu dataForRowAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath{
    if (dropIndexPath.row == 3) {
        return @[@""];
    }else if (dropIndexPath.row == 0){
        return @[@"微信",@"支付宝",@"云闪付"];
    }else if (dropIndexPath.row == 1){
            return @[@"台卡",@"收银APP",@"百富"];
    }else if (dropIndexPath.row == 2){
            return @[@"昨日",@"本周",@"本月"];
    }else if (dropIndexPath.row == 4){
        return @[@"收款",@"退款"];
    }
    NSMutableArray *names = [[NSMutableArray alloc] init];
    for (LKAccountModel *model in self.rolesArray) {
        [names addObject:model.user_name];
    }
    return names;

}

- (NSInteger)menu:(WMZDropDownMenu *)menu countForRowAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath{
    if (dropIndexPath.row == 3) {
        return 1;
    }
    return 3;
}

#define titleArr @[@"按付款方式",@"按付款渠道",@"按交易时间",@"",@"交易类型",@"员工"]
- (NSString*)menu:(WMZDropDownMenu *)menu titleForHeadViewAtDropIndexPath:(WMZDropIndexPath*)dropIndexPath{
        return titleArr[dropIndexPath.row];
}


- (CGFloat)menu:(WMZDropDownMenu *)menu heightForFootViewAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath{
    return 10;
}

- (CGFloat)menu:(WMZDropDownMenu *)menu heightForHeadViewAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath{
    return 30;
}
-(CGFloat)menu:(WMZDropDownMenu *)menu heightAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath AtIndexPath:(NSIndexPath *)indexpath{
    if (dropIndexPath.row == 3) {
        return 80;
    }
    return 30;
}
- (MenuUIStyle)menu:(WMZDropDownMenu *)menu uiStyleForRowIndexPath:(WMZDropIndexPath *)dropIndexPath{
    return MenuUICollectionView;

}

- (MenuEditStyle)menu:(WMZDropDownMenu *)menu editStyleForRowAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath{
    return MenuEditOneCheck;
}

/*
*返回section行标题数据视图消失的动画样式   默认 MenuHideAnimalNone
*/
- (MenuHideAnimalStyle)menu:(WMZDropDownMenu *)menu hideAnimalStyleForRowInSection:(NSInteger)section{
            return MenuHideAnimalLeft;
}

- (MenuShowAnimalStyle)menu:(WMZDropDownMenu *)menu showAnimalStyleForRowInSection:(NSInteger)section{
            return MenuShowAnimalRight;
}

- (BOOL)menu:(WMZDropDownMenu *)menu closeWithTapAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath{
    return NO;
}

-(UICollectionViewCell *)menu:(WMZDropDownMenu *)menu cellForUICollectionView:(WMZDropCollectionView *)collectionView AtDropIndexPath:(WMZDropIndexPath *)dropIndexPath AtIndexPath:(NSIndexPath *)indexpath dataForIndexPath:(WMZDropTree *)model{
    if (dropIndexPath.section == 0 || dropIndexPath.section == 1 ) {
        NSLog(@"%ld",dropIndexPath.row);
        if (dropIndexPath.row == 3) {
            LKDatePickeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LKDatePickeCell" forIndexPath:indexpath];
            if (self.begin_time.length != 0) {
               [cell.startTimeLabel setTitle:self.begin_time forState:UIControlStateNormal];
            }
            if (self.end_time.length != 0) {
                [cell.endTimeLabel setTitle:self.end_time forState:UIControlStateNormal];
            }

                [cell.startTimeLabel addTarget:self action:@selector(startTimeAction:) forControlEvents:UIControlEventTouchUpInside];
                [cell.endTimeLabel addTarget:self action:@selector(endTimeAction:) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }

    }

    return nil;
}

/// 开始时间的选择
/// @param btn 收拾
-(void)startTimeAction:(UIButton *)btn{
    WS(weakSelf)
    [GFDatePicker showCustomDatePickerAtView:[UIApplication sharedApplication].keyWindow WithFomate:@"" choosedDateBlock:^(NSDate *date) {
        weakSelf.type = @"8";
        NSString *dateStr = [NSString getTimeStrAccoringDate:date];
         NSString *timeStr1 = [dateStr componentsSeparatedByString:@" "][0];
         NSString *timeStr2 = [dateStr componentsSeparatedByString:@" "][1];
        NSString *timeStr22 = [NSString stringWithFormat:@"%@:%@",[timeStr2 componentsSeparatedByString:@":"][0],[timeStr2 componentsSeparatedByString:@":"][1]];
         NSString *timeStr = [NSString stringWithFormat:@"%@ %@",timeStr1,timeStr22];
         [btn setTitle:timeStr forState:UIControlStateNormal];
        weakSelf.begin_time = timeStr;
    } cancelBlock:^{

    }];
}

/// 结束时间的选择
/// @param btn 点击收拾
-(void)endTimeAction:(UIButton *)btn{
    WS(weakSelf)
    [GFDatePicker showCustomDatePickerAtView:[UIApplication sharedApplication].keyWindow WithFomate:@"" choosedDateBlock:^(NSDate *date) {
        weakSelf.type = @"8";
        NSString *dateStr = [NSString getTimeStrAccoringDate:date];
         NSString *timeStr1 = [dateStr componentsSeparatedByString:@" "][0];
         NSString *timeStr2 = [dateStr componentsSeparatedByString:@" "][1];
        NSString *timeStr22 = [NSString stringWithFormat:@"%@:%@",[timeStr2 componentsSeparatedByString:@":"][0],[timeStr2 componentsSeparatedByString:@":"][1]];
         NSString *timeStr = [NSString stringWithFormat:@"%@ %@",timeStr1,timeStr22];
         [btn setTitle:timeStr forState:UIControlStateNormal];
        weakSelf.end_time = timeStr;
    } cancelBlock:^{
        
    }];
}
- (void)menu:(WMZDropDownMenu *)menu customDefauultCollectionFootView:(WMZDropConfirmView *)confirmView{
    confirmView.showBorder = YES;
    confirmView.resetBtn.backgroundColor = [UIColor whiteColor];
    [confirmView.confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
    confirmView.confirmBtn.backgroundColor = [UIColor redColor];
    [confirmView.resetBtn addTarget:self action:@selector(reSetAction) forControlEvents:UIControlEventTouchUpInside];
    [confirmView.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
-(void)reSetAction{
    self.begin_time = @"";
    self.end_time = @"";
    self.paystyle = @"";
    self.status = @"";
    self.key_word = @"";
    self.mode = @"";
    self.type = @"0";
    self.checker_id = @"";
//    WMZDropIndexPath *indexsection = [[WMZDropIndexPath alloc] initWithSection:1 row:3];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
    LKDatePickeCell *cell = [_menu.collectionView dequeueReusableCellWithReuseIdentifier:@"LKDatePickeCell" forIndexPath:indexPath];
    [cell.startTimeLabel setTitle:@"请设置时间" forState:UIControlStateNormal];
    [cell.endTimeLabel setTitle:@"请设置时间" forState:UIControlStateNormal];
    [_menu reSetAction];
}
-(void)menu:(WMZDropDownMenu *)menu didSelectRowAtDropIndexPath:(WMZDropIndexPath *)dropIndexPath dataIndexPath:(NSIndexPath *)indexpath data:(WMZDropTree *)data{
        if (dropIndexPath.section == 0 || dropIndexPath.section == 1 ) {
        NSLog(@"你点击了%ld",dropIndexPath.row);
        if (dropIndexPath.row == 0) {
            if (data.isSelected) {
                  self.paystyle = [NSString backPayDataStyleAccordingModelStr:data.name];
            }else{
                self.paystyle = @"";
            }
          
        }
        if (dropIndexPath.row == 1) {
            if (data.isSelected) {
                self.mode = [NSString backPayDataModeStrAccordingModel:data.name];
            }else{
                self.mode = @"";
            }
            
        }
        if (dropIndexPath.row == 2) {
            if (data.isSelected) {
               self.type = [self backStatuValueAccordingStatusStr:data.name];
            }else{
                self.type = @"";
            }
            
        }
        if (dropIndexPath.row == 3) {
            
        }
        if (dropIndexPath.row == 4) {
            if (data.isSelected) {
                self.status = [self backTimeValueAccordingStatusStr:data.name];
            }else{
                self.status = @"";
            }
            
        }
        if (dropIndexPath.row == 5) {
            if (data.isSelected) {
                for (LKAccountModel *model in self.rolesArray) {
                    if ([model.user_name isEqualToString:data.name]) {
                        self.checker_id = model.Id;
                    }
                }
            }else{
                self.checker_id = @"";
            }
        }
            

    }
}
- (void)menu:(WMZDropDownMenu *)menu didConfirmAtSection:(NSInteger)section selectNoramelData:(NSMutableArray *)selectNoramalData selectStringData:(NSMutableArray *)selectData{
    self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"type":self.type,@"begin_time":self.begin_time,@"end_time":self.end_time,@"paystyle":self.paystyle,@"status":self.status,@"mode":self.mode,@"checker_id":self.checker_id,@"key_word":self.key_word}];
    self.page = 1;
    [self getPayBillListWithTypeParam];
    NSLog(@"选中的数据%@",selectData);
}
-(NSMutableArray *)updateDataArray{
    if (_updateDataArray == nil) {
        _updateDataArray = [[NSMutableArray alloc] init];
    }
    return _updateDataArray;
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
-(NSMutableArray *)rolesArray{
    if (_rolesArray == nil) {
        _rolesArray = [[NSMutableArray alloc] init];
    }
    return _rolesArray;
}
-(void)getRolesArrayData{
    NSMutableDictionary * param = [[NSMutableDictionary alloc] initWithDictionary:@{@"page":@"0",@"is_search":@"1",@"role_id":[LKUserInforTool account].pid}];
    NSString *sign = [NSString encoingWithDic:param];
    [param setObject:sign forKey:@"sign"];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShaiXuanDataUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            NSArray *dataDic = responseObject[@"data"][@"data"];
            for (NSDictionary *dic in dataDic) {
                LKAccountModel *model = [LKAccountModel modelWithDictionary:dic];
                [self.rolesArray addObject:model];
            }
            [self setNavgationRightBtn];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(void)getPayBillListWithTypeParam{
    NSString *url;
    if (self.cate_id) {
        url = MyCardBillListUrl;
    }else{
        url = MyPayBillListUrl;
        if (self.begin_time.length != 0){
            if (self.end_time.length == 0) {
                [SVProgressHUD showInfoWithStatus:@"时间设置不完整"];
                return;
            }
        }
        if (self.end_time.length != 0){
            if (self.begin_time.length == 0) {
                [SVProgressHUD showInfoWithStatus:@"时间设置不完整"];
                return;
            }
        }
        if ([self.end_time floatValue] < [self.begin_time floatValue]){
            [SVProgressHUD showInfoWithStatus:@"结束时间不能早于开始时间"];
            return;
        }
    }

    NSString *sign = [NSString encoingWithDic:self.paramDic];
    [self.paramDic setObject:sign forKey:@"sign"];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:url header:[LKUserInforTool account].token parameters:self.paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            [self.updateDataArray removeAllObjects];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (self.page == 1) {
                [self.dataArray removeAllObjects];
            }

            NSArray *dataDic = responseObject[@"data"][@"detail"];
            NSInteger totalCount = [responseObject[@"data"][@"total_page"] integerValue];
            if (dataDic.count < 13) {
                self.tableView.mj_footer.hidden = YES;
            }
            for (NSDictionary *dic in dataDic) {
                LKBillListModel *model = [LKBillListModel modelWithDictionary:dic];
                [self.dataArray addObject:model];
            }

            if (self.dataArray.count  != 0) {
                [self arrayGroup:self.dataArray];
            }
            [self.tableView reloadData];
            
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}


/// 模型分类排序
/// @param dataArray 数组元素
- (void)arrayGroup:(NSArray *)dataArray {
    [self.updateDataArray removeAllObjects];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if(dataArray) {
        //遍历
        [dataArray enumerateObjectsUsingBlock:^(LKBillListModel * model, NSUInteger idx, BOOL * _Nonnull stop) {
            NSMutableArray *array = dic[model.bill_date];
            if(!array)
                //如果数组不存在创建
                array = @[model].mutableCopy;
            else
              //如果存在添加元素
                [array addObject:model];
           //根据不同的key,创建对应的字典
            [dic setObject:array forKey:model.bill_date];
        }];
        NSLog(@"分组后的字典%@",dic);
        NSMutableArray *data = [NSMutableArray new];
        NSArray *sortKeys =[dic.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
                return [obj2 compare:obj1];
          }];
        for (NSString *key in sortKeys) {
            [data addObject:@{@"date":key,@"list":dic[key]}];
        }
        NSLog(@"排序后的%@",data);
//        self.updateDataArray = [data mutableCopy];
        [self.updateDataArray addObjectsFromArray:data];
    }
}

/// 根据交易状态返回相对应的状态
/// @param statusStr 交易状态
-(NSString *)backStatuValueAccordingStatusStr:(NSString *)statusStr{
    if ([statusStr isEqualToString:@"昨日"]) {
        return @"2";
    }else if ([statusStr isEqualToString:@"本周"]){
        return @"3";
    }else if ([statusStr isEqualToString:@"本月"]){
        return @"4";
    }
    return @"";
}

/// 根据交易时间返回相对应的状态值
/// @param timeStr 时间str
-(NSString *)backTimeValueAccordingStatusStr:(NSString *)timeStr{
    if ([timeStr isEqualToString:@"收款"]) {
        return @"1";
    }else if ([timeStr isEqualToString:@"退款"]){
        return @"2";
    }
    return @"";
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@implementation LKDatePickeCell


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.startView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height/2)];
        [self.contentView addSubview:self.startView];
        LKBaseLabel *starttitleLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"开始时间" textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] textAlignment:NSTextAlignmentLeft];
        [self.startView addSubview:starttitleLabel];
        [starttitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.startView);
            make.centerY.equalTo(self.startView);
        }];
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.image = [UIImage imageNamed:@"mine_Arrowin"];
        [self.startView addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.startView.mas_right);
            make.centerY.equalTo(self.startView);
            make.size.mas_equalTo(CGSizeMake(10, 15));
        }];
        self.startTimeLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.startTimeLabel setTitle:@"请设置时间" forState:UIControlStateNormal];
        self.startTimeLabel.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
        [self.startTimeLabel setTitleColor:COLOR_HEX(0x666666) forState:UIControlStateNormal];
        [self.startView addSubview:self.startTimeLabel];
        [self.startTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(imageV.mas_left).offset(-10);
            make.centerY.equalTo(self.startView);

        }];
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = viewDefualtColor;
        [self.startView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.startView);
            make.height.mas_equalTo(1);
        }];
        self.endView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentView.bounds.size.height/2, self.contentView.bounds.size.width, self.contentView.bounds.size.height/2)];
        [self.contentView addSubview:self.endView];
        LKBaseLabel *endtitleLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"结束时间" textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] textAlignment:NSTextAlignmentLeft];
        [self.endView addSubview:endtitleLabel];
        [endtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.endView);
            make.centerY.equalTo(self.endView);
        }];
        UIImageView *imageV1 = [[UIImageView alloc] init];
        imageV1.image = [UIImage imageNamed:@"mine_Arrowin"];
        [self.endView addSubview:imageV1];
        [imageV1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.endView.mas_right);
            make.centerY.equalTo(self.endView);
            make.size.mas_equalTo(CGSizeMake(10, 15));
        }];
        self.endTimeLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.endTimeLabel setTitle:@"请设置时间" forState:UIControlStateNormal];
        self.endTimeLabel.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
        [self.endTimeLabel setTitleColor:COLOR_HEX(0x666666) forState:UIControlStateNormal];
        [self.endView addSubview:self.endTimeLabel];
        [self.endTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(imageV1.mas_left).offset(-10);
            make.centerY.equalTo(self.endView);
        }];
    }
    return self;
}



@end
